#include "crc16.h"

static u16 crc_table[256];

void
init_crc16_table (void)
{
  int i, j;
  u16 k;

  for (i = 0; i < 256; i++)
    {
      k = 0xC0C1;
      for (j = 1; j < 256; j <<= 1)
	{
	  if (i & j)
	    crc_table[i] ^= k;
	  k = (k << 1) ^ 0x4003;
	}
    }
}



u16
crc16 (char *buf, unsigned int nbytes)
{
  u16 crc = 0;
  unsigned char *p, *lim;

  p = (unsigned char *) buf;
  lim = p + nbytes;
  while (p < lim)
    {
      crc = (crc >> 8) ^ crc_table[(crc & 0xFF) ^ *p++];
    }
  return crc;
}
